package xyz.msj.hrm.util;

import org.csource.fastdfs.*;

@SuppressWarnings("Duplicates")
public class FastDfsUtil {

    //加载fastdfs配置文件
    public static String CONF_FILENAME  = FastDfsUtil.class.getClassLoader()
            .getResource("fdfs_client.conf").getFile();

    /**
     * 上传文件
     * @param file ： 文件，byte[]格式
     * @param extName ：扩展名
     * @return
     */
    public static  String upload(byte[] file,String extName) {
        try {
            //初始化配置
            ClientGlobal.init(CONF_FILENAME);
            //创建Tracker客户端
            TrackerClient tracker = new TrackerClient();
            //获取一个Tracker服务
            TrackerServer trackerServer = tracker.getTrackerServer();
            //创建一个Storage服务
            StorageServer storageServer = null;
            //得到文件存储对象的客户端
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
            //执行上传 ：文件，扩展名 ， 额外参数
            //返回：文件的路径(groupName 组名, path 文件路径)
            String fileIds[] = storageClient.upload_file(file,extName,null);
            //  /group1/M00/00/00/dhma1l6ecFeAckIhAABGKnIct1M652.jpg
            String filePath =   "/"+fileIds[0]+"/"+fileIds[1];
            System.out.println("文件上传成功："+filePath);
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
    }
    /**
     * 上传文件
     * @param path :文件的绝对路径  d:/xxx.jpg
     * @param extName：扩展名
     * @return
     */
    public static  String upload(String path,String extName) {
 
        try { 
            ClientGlobal.init(CONF_FILENAME);
            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getTrackerServer();
            StorageServer storageServer = null;
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
            String fileIds[] = storageClient.upload_file(path, extName,null);
            return  "/"+fileIds[0]+"/"+fileIds[1];
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
    }

    /**
     * 下载文件
     * @param groupName :组名
     * @param fileName ：文件路径
     * @return
     */
    public static byte[] download(String groupName,String fileName) {
        try {
            ClientGlobal.init(CONF_FILENAME);
            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getTrackerServer();
            StorageServer storageServer = null;
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
            byte[] b = storageClient.download_file(groupName, fileName);
            return  b;
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        } 
    }
     

    /**
     * 删除文件
     * @param groupName
     * @param fileName
     */
    public static boolean delete(String groupName,String fileName){
        try { 
            ClientGlobal.init(CONF_FILENAME);
            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getTrackerServer();
            StorageServer storageServer = null;
            StorageClient storageClient = new StorageClient(trackerServer,
                    storageServer); 
            int i = storageClient.delete_file(groupName,fileName);
            System.out.println( i==0 ? "删除成功" : "删除失败:"+i);
            return i == 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw  new RuntimeException("删除异常,"+e.getMessage());
        } 
    }
}